/**
 * (C) 2007 - Boston University
 * 	Contact: Michael Ocean 

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
package snbenchlib;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;

import snbenchlib.Trace;
import sxe.Gui;

/**
 * This Log is a "global" class for access to trace/logging mechanisms
 * classes wishing to Log should invoke one of the following, as appropriate:
 * 
 * Log.evaluator
 * Log.scheduler
 * Log.server
 * Log.graph
 * Log.hash
 * Log.misc
 * 
 * @author Michael
 *
 */
public class Log {
	/** Put the log settings in one place 
	 *  for easy access and modification */
	
	private static final int EVAL_TRACELEVEL = Trace.DEBUG;
	private static final int SCHED_TRACELEVEL = Trace.DEBUG;
	private static final int SERVER_TRACELEVEL = Trace.DEBUG;
	private static final int GRAPH_TRACELEVEL = Trace.DEBUG;
	private static final int HASH_TRACELEVEL = Trace.DEBUG;
	private static final int JANITOR_TRACELEVEL = Trace.MINIMAL;
	private static final int MISC_TRACELEVEL = Trace.DEBUG;
	
	private static final int RM_TRACELEVEL = Trace.DEBUG;
	private static final int SD_TRACELEVEL = Trace.DEBUG;
	
	private static final String FILE = "out";
	
	private static final String EVAL_PREFIX = "[eval]:";
	private static final String SCHED_PREFIX = "[sched]:";
	private static final String SERVER_PREFIX = "[server]:";
	private static final String GRAPH_PREFIX = "[graph]:";
	private static final String HASH_PREFIX = "[hash]:";
	private static final String JANITOR_PREFIX = "[janitor]:";
	private static final String MISC_PREFIX = "[misc]:";
	
	private static final String RM_PREFIX = "[RM]:";
	private static final String SD_PREFIX = "[SD]:";
	
	public static Trace evaluator;
	public static Trace scheduler;
	public static Trace server; 
	public static Trace graph;
	public static Trace hash;
	public static Trace janitor;
	public static Trace misc;
	public static Trace rm;
	public static Trace sd;
	
	/* actually create the logs */
	/*
	/public static Trace evaluator = new Trace(EVAL_PREFIX,FILE,EVAL_TRACELEVEL);
	public static Trace scheduler = new Trace(SCHED_PREFIX,FILE,SCHED_TRACELEVEL);
	public static Trace server = new Trace(SERVER_PREFIX,FILE,SERVER_TRACELEVEL); 
	public static Trace graph = new Trace(GRAPH_PREFIX,FILE,GRAPH_TRACELEVEL);
	public static Trace hash = new Trace(HASH_PREFIX,FILE,HASH_TRACELEVEL);
	public static Trace janitor = new Trace(JANITOR_PREFIX,FILE,JANITOR_TRACELEVEL);
	public static Trace misc = new Trace(MISC_PREFIX,FILE,MISC_TRACELEVEL);

	public static Trace rm = new Trace(RM_PREFIX,FILE,RM_TRACELEVEL);
	public static Trace sd = new Trace(SD_PREFIX,FILE,SD_TRACELEVEL);
	*/
	
	private Log()
		{};
	
	public static void InitLogs(sxe.Gui gui){
		if(gui==null){		
			evaluator = new Trace(EVAL_PREFIX,FILE,EVAL_TRACELEVEL);
			scheduler = new Trace(SCHED_PREFIX,FILE,SCHED_TRACELEVEL);
			server = new Trace(SERVER_PREFIX,FILE,SERVER_TRACELEVEL); 
			graph = new Trace(GRAPH_PREFIX,FILE,GRAPH_TRACELEVEL);
			hash = new Trace(HASH_PREFIX,FILE,HASH_TRACELEVEL);
			janitor = new Trace(JANITOR_PREFIX,FILE,JANITOR_TRACELEVEL);
			misc = new Trace(MISC_PREFIX,FILE,MISC_TRACELEVEL);
			rm = new Trace(RM_PREFIX,FILE,RM_TRACELEVEL);
			sd = new Trace(SD_PREFIX,FILE,SD_TRACELEVEL);
		}
		else{
			evaluator = new Trace(EVAL_PREFIX,gui,EVAL_TRACELEVEL);
			scheduler = new Trace(SCHED_PREFIX,gui,SCHED_TRACELEVEL);
			server = new Trace(SERVER_PREFIX,gui,SERVER_TRACELEVEL); 
			graph = new Trace(GRAPH_PREFIX,gui,GRAPH_TRACELEVEL);
			hash = new Trace(HASH_PREFIX,gui,HASH_TRACELEVEL);
			janitor = new Trace(JANITOR_PREFIX,gui,JANITOR_TRACELEVEL);
			misc = new Trace(MISC_PREFIX,gui,MISC_TRACELEVEL);
			rm = new Trace(RM_PREFIX,gui,RM_TRACELEVEL);
			sd = new Trace(SD_PREFIX,gui,SD_TRACELEVEL);			
		}
	}
	
    /**
     * Opens a file handle to read configuration from properties 
     * file
     * @return
     */

    private Properties openPropsFile(){
    	
    	// EFFECTS:  opens properties file for reading trace configuration parameters.
    	Properties props = new Properties();
    	
    	InputStream in = props.getClass().getResourceAsStream("/trace.properties");
		
		if (in!=null) {
			try {
     			props.load(in);
				in.close();
			}
			catch(IOException e) {
				System.err.println("Trace: Opening properties file unsuccessful " + e);
			}
			return props;
		}
    	return null;
    }
       
    /**
     * assigns all the variables to values obtained from properties 
     * file
     * @param props
     */
    public void setVar(Properties props) {
		boolean trace;
		String filename;
		int tracelevel;
		
	 	 if (props.containsKey("trace")) {
	 			String val = props.getProperty("trace");
				if (val.compareTo("1")==0) 
					trace = true;
				
			}
				
			if (props.containsKey("tracefile")) {
				String val = props.getProperty("tracefile");
				if(val.length() > 0) {
					filename = val;
				}

			}
				
			if (props.containsKey("tracelevel")) {
				String val = props.getProperty("tracelevel");
				
				if (val.length() > 0) {
				    try {
				        tracelevel = Integer.parseInt(val);
				    } catch (NumberFormatException n) {
					    n.printStackTrace();
				    }
				} else 
					tracelevel = 1;
							
			}   	
    }
              
}
